<?php

namespace App\Admin\Controllers;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use \App\Models\PointsProduct;
use \App\Models\Coupon;

class PointsProductsController extends AdminController
{
    protected $title = '积分商品管理';
    
    protected function grid()
    {
        $grid = new Grid(new PointsProduct());

	    $grid->disableExport();
	
	    $grid->column('id', 'ID');
	    $grid->column('title', '商品名称');
	    $grid->column('coupon_id', '优惠卷名称')->display(function ($value) {
		   return isset($this->coupon) ? $this->coupon->name : '';
	    });
	    $grid->column('image', '图片')->lightbox(['width' => 50, 'height' => 50]);

	    $grid->column('price', '原价');
	    $grid->column('stock', '库存');
	    $grid->column('quantity', '每人每日可兑数量');
	    $grid->column('points', '消耗积分');
	   
	    $grid->column('created_at', '创建时间')->display(function () {
		    return $this->created_at->toDatetimeString();
	    });
	
	    $grid->disableRowSelector();

        return $grid;
    }
	
	 protected function detail($id)
    {
        $show = new Show(PointsProduct::findOrFail($id));
        return $show;
    }
	
    protected function form()
    {
        $form = new Form(new PointsProduct());

	    $form->text('title', '商品名称')->rules('required');
	    $form->select('coupon_id','优惠卷')->options(Coupon::all()->pluck('name', 'id'));
	    $form->image('image', '图片')->uniqueName()->help('NOTE：保证清晰的前提下尽可能压缩体积');
	    $form->currency('price', '原价')
				    ->symbol('￥')
				    ->rules('numeric');
	    $form->currency('points', '积分')->rules('numeric|required');
	    $form->number('stock', '库存')->rules('numeric');
	    $form->number('quantity', '每人可兑换数量')->rules('numeric');
	    $form->textarea('desc', '商品描述')->rules('required');

	    
        return $form;
    }
}
